package com.springboot.springboot_demo02.controller;

import com.springboot.springboot_demo02.pojo.Article;
import com.springboot.springboot_demo02.service.ArticleService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/article")
    public List<Article> findAll(){
        List<Article> list = articleService.findAll();
        return list;
    }
    @GetMapping("/article/{id}")
    public List<Article> findById(@PathVariable Integer id){
        List<Article> list = articleService.findById(id);
        return list;
    }
    @PutMapping("/article")
    public String update(Article article){
      int row = articleService.update(article);
      if (row>0){
          return "update success";
      }else{
          return "update error";
      }
    }
    @DeleteMapping("/article/{id}")
    public String delete( @PathVariable  Integer id){
        int row = articleService.delete(id);
        if (row>0){
            return "delete success";
        }else{
            return "delete error";
        }
    }
    @PostMapping("/article")
    public String save(Article article){
        int row = articleService.save(article);
        if (row>0){
            return "save success";
        }else{
            return "save error";
        }
    }
}
